<?php

class LoginForm extends BaseForm
{

    public function configure()
    {
        $this->setWidgets(array('username' => new sfWidgetFormInput(), 'password' => new sfWidgetFormInputPassword()));
        $this->setValidators(array('username' => new sfValidatorPass(), 'password' => new sfValidatorPass()));
        $this->getValidatorSchema()->setPostValidator(
                new sfValidatorCallback(array('callback' => array($this, 'checkLoginData')))
        );
        $this->getWidgetSchema()->setNameFormat('login[%s]');
        $this->addFormatter('customFormatter');
    }

    public function checkLoginData($validator, $values)
    {
        $flag = false;
        if ($values['username'] && $values['password'])
        {
            $settings = SettingsTable::getInstance()->findOneByUsernameAndPassword($values['username'], Settings::encrypt($values['password']));
            if ($settings)
            {
                $this->settings = $settings;
                $flag           = true;
            }
        }
        if (!$flag)
        {
            // password is not correct, throw an error
            throw new sfValidatorError($validator, 'invalid username and password');
        }
        // password is correct, return the clean values
        return $values;
    }

}